
#include "Cfgread.h"
#include <QFile>
#include <QCoreApplication>
#include <QFileInfo>
#include <QDIR>
#include "logger_data.h"

#define CONFIG_PATH "/config/logCfg.json"

CfgRead::CfgRead()
{
	m_setting.debug = 0;
	m_setting.types.append("common");
	m_setting.level = 0;
	m_setting.limit = 0;
	m_setting.cache = 10;
	m_setting.path = QCoreApplication::applicationDirPath() + "/log/";
}
CfgRead::~CfgRead()
{}

bool CfgRead::Init()
{
	QString filename = QCoreApplication::applicationDirPath() + QString(CONFIG_PATH);
	QFile file(filename);
	if (!file.exists()) {
		return false;
	}
	if (!file.open(QIODevice::ReadOnly)) {
		return false;
	}
	QByteArray data = file.readAll();
	LogCfg cfg = {};
	cfg.fromJson(data);
	m_setting.debug = cfg.debug();
	for (auto& iter : cfg.types()) {
		m_setting.types.append(iter);
	}
	m_setting.types.removeDuplicates();
	if (!cfg.path().isEmpty()) {
		m_setting.path = cfg.path();
	}
	m_setting.level = cfg.level();
	m_setting.limit = cfg.limit();
	file.close();
	return true;
}
LogSet CfgRead::GetLogSetting()
{


	return m_setting;
}
